package com.cmict.data.form.api.util;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cmict.data.form.api.service.SysCurrencyService;
import com.cmict.data.form.api.service.SysFormService;
import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.infra.api.rdb.RdbApi;
import com.cmict.data.system.api.dept.DeptApi;
import com.cmict.data.system.api.dept.PostApi;
import com.cmict.data.system.api.dept.dto.DeptRespDTO;
import com.cmict.data.system.api.dict.DictDataApi;
import com.cmict.data.system.api.dict.dto.DictDataDTO;
import com.cmict.data.system.api.user.AdminUserApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.*;

/**
 * @description: 导入字段类型工具类
 * @author: qiaoyu
 * @create: 2022-07-08 22:57
 **/
@Component
public class IsDepartUtil {

    @Autowired
    private SysFormService sysFormService;
    private static SysFormService realsysFormService;

    @Autowired
    private SysCurrencyService sysCurrencyService;
    private static SysCurrencyService realsysCurrencyService;

    @Autowired
    private DictDataApi dictDataService;
    private static DictDataApi realdictDataService;

    @Autowired
    private DeptApi deptService;
    private static DeptApi realdeptService;

    @Autowired
    private AdminUserApi adminUserService;
    private static AdminUserApi realadminUserService;

    @Autowired
    private PostApi postService;
    private static PostApi realpostService;

    @Autowired
    private RdbApi rdbApiService;
    private static RdbApi realrdbapiService;

    @PostConstruct
    public void init() {
        realsysFormService = sysFormService;
        realsysCurrencyService = sysCurrencyService;
        realdictDataService = dictDataService;
        realdeptService = deptService;
        realadminUserService = adminUserService;
        realpostService = postService;
        realrdbapiService = rdbApiService;
    }
    static Map<Object,Object> departDataMapping(JSONObject field,String value) {
        String req = "";
        boolean isselect = true;
        String returnValue = "";
        //定义参数
        Map<Object,Object> mapStatic = staticDepart(req,isselect,returnValue,field,value);
        req = (String) mapStatic.get("req");
        isselect = (boolean) mapStatic.get("isselect");
        returnValue = (String) mapStatic.get("returnValue");
        //全部
        Map<Object,Object> map = new HashMap<>();
        map.put("req",req);
        map.put("isselect",isselect);
        map.put("value",returnValue);
        return map;
    }
    static Map<Object,Object> staticDepart(String req,Boolean isselect,String returnValue,JSONObject field,String value) {
        //部门数据
        Boolean ret = Boolean.valueOf(field.getString("multiple"));
        if(ret){
            List<String> values = Arrays.asList(value.split(","));
            if(ObjectUtil.isNotEmpty(values)){
                for (int i = 0; i <values.size() ; i++) {
                    List<String> daparts = Arrays.asList(values.get(i).split("/"));
                    if(ObjectUtil.isNotEmpty(daparts)){
                        List<DeptRespDTO> depts = realdeptService.selectListAll().getCheckedData();
                        DeptRespDTO deptprevious = new DeptRespDTO();
                        for (int j = 0; j < daparts.size() ; j++) {
                            for (int k = 0; k <depts.size() ; k++) {
                                if(depts.get(k).getName().equals(daparts.get(j)) && depts.get(k).getParentId() == 0){
                                    deptprevious = depts.get(k);
                                }else{
                                    if(depts.get(k).getName().equals(daparts.get(j))){
                                        if(ObjectUtil.isNotEmpty(depts.get(k)) && ObjectUtil.isNotEmpty(deptprevious) && String.valueOf(depts.get(k).getParentId()).equals(String.valueOf(deptprevious.getId()))){
                                            deptprevious = depts.get(k);
                                        }
                                    }
                                }
                            }
                        }
                        if(ObjectUtil.isNotEmpty(deptprevious)){
                            returnValue = String.valueOf(deptprevious.getId());
                        }
                    }
                }
                returnValue = returnValue + ",";
            }
            if(ObjectUtil.isNotEmpty(returnValue)){
                if(returnValue.substring(returnValue.length()-1).equals(",")){
                    returnValue = returnValue.substring(0,returnValue.length() - 1);
                }
            }
        }else{
            List<String> daparts = Arrays.asList(value.split("/"));
            if(ObjectUtil.isNotEmpty(daparts)){
                List<DeptRespDTO> depts = realdeptService.selectListAll().getCheckedData();
                DeptRespDTO deptprevious = new DeptRespDTO();
                for (int j = 0; j < daparts.size() ; j++) {
                    Boolean isnull = false;
                    if(isselect){
                        for (int k = 0; k <depts.size() ; k++) {
                            if(depts.get(k).getName().equals(daparts.get(j)) && depts.get(k).getParentId() == 0){
                                deptprevious = depts.get(k);
                                isnull = true;
                            }else{
                                if(depts.get(k).getName().equals(daparts.get(j))){
                                    if(ObjectUtil.isNotEmpty(depts.get(k)) && ObjectUtil.isNotEmpty(deptprevious) && String.valueOf(depts.get(k).getParentId()).equals(String.valueOf(deptprevious.getId()))){
                                        deptprevious = depts.get(k);
                                        isnull = true;
                                    }
                                }
                            }
                        }
                        if(!isnull){
                            req = "：" + daparts.get(j) + "不存在;";
                            isselect = false;
                        }
                    }
                }
                if(ObjectUtil.isNotEmpty(deptprevious)){
                    returnValue = String.valueOf(deptprevious.getId());
                }
            }
            List<String> values = Arrays.asList(value.split(","));
            if(values.size()>1){
                req = "字段为单选;";
                isselect = false;
            }
        }
        Map<Object,Object> map = new HashMap<>();
        map.put("req",req);
        map.put("isselect",isselect);
        map.put("returnValue",returnValue);
        return map;
    }
    /**
     *列表类型
     */
    @Transactional(rollbackFor = Exception.class)
    public static Boolean treeList(String json) {
        JSONObject personData = JSON.parseObject(json);
        JSONObject rightConfig = personData.getJSONObject("rightConfig");
        String panelType = rightConfig.getString("panelType");
        if(panelType.equals("tree-list")){ return false; }
        return true;
    }
}

